package com.jd.charactercalculator.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.springframework.beans.BeanUtils;
import com.jd.charactercalculator.dao.UserDao;
import com.jd.charactercalculator.dto.param.UserParamDto;
import com.jd.charactercalculator.dto.result.PagingResultDto;
import com.jd.charactercalculator.dto.result.UserResultDto;
import com.jd.charactercalculator.entity.UserEntity;
import com.jd.charactercalculator.service.UserService;

/**
 * 
 *
 * @author:孙林
 * @since:2012上午11:06:52
 * @version:1.0.0
 */
public class UserServiceImpl implements UserService {
	
	private UserDao userDao;
	
	@Override
	public PagingResultDto<UserResultDto> getUsers(UserParamDto userParamDto) {
		//返回结果对象
		PagingResultDto<UserResultDto> pagingResultDto = new PagingResultDto<UserResultDto>();
		List<UserResultDto> userResultDtoList = new ArrayList<UserResultDto>();
		
		//查询user表
		UserEntity userEntity1 = new UserEntity();
		BeanUtils.copyProperties(userParamDto, userEntity1);
		List<UserEntity> userEntityList =  userDao.getUsers(userEntity1);
		
		//逻辑处理，将Entity转乘dto
		Iterator<UserEntity> iterator =  userEntityList.iterator();
		while (iterator.hasNext()) {
			UserEntity userEntity =  iterator.next();
			UserResultDto userResultDto = new UserResultDto();
			//copy属性,可以用spring 的 BeanUtils
			//或者commons-beanutils-1.8.3.jar
			//commons-beanutils-bean-collections-1.8.3.jar
			//commons-beanutils-core-1.8.3.jar
			
//			userResultDto.setUsername(userEntity.getUsername());
//			userResultDto.setPassword(userEntity.getPassword());
			BeanUtils.copyProperties(userEntity, userResultDto);
		}
		
		//放入PagingResultDto中
		pagingResultDto.setList(userResultDtoList);
		pagingResultDto.setTotalSize(userResultDtoList.size());
		
		return pagingResultDto;
	}
	
	@Override
	public boolean addUsers(List<UserParamDto> userParamDtoList) {
		boolean result = false;
		
		//遍历list,转换成UserEntity
		
		Iterator iterator = userParamDtoList.iterator();
		while (iterator.hasNext()) {
			UserParamDto userParamDto = (UserParamDto) iterator.next();
			UserEntity userEntity = new UserEntity();
			BeanUtils.copyProperties(userParamDto, userEntity);
			userDao.addUser(userEntity);
		}
		
		//没有报异常，返回成功 
		result = true;
		return result;
	}

	@Override
	public boolean updateUsers(List<UserParamDto> userParamDtoList) {
		return false;
	}
	
	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}



}
